﻿#region Copyright Description
/**********************************************************
(C) Copyright 奇科计算机信息有限公司. 2009
FileName      : CFile.cs
Function        : 系统中常用的文件操作类
Author           : 代成华
Last modified by : 代成华
Last modified    : 2009-11-13
************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
namespace System
{
    /// <summary>
    /// 系统中常用的文件操作类

    /// </summary>
    public class CFile
    {
        public static string filePathName = "";
        public static string errorFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "errorFile\\";
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="filePath">文件夹目录</param>
        /// <param name="fileName">文件名称</param>
        public static void CreateTxtFile(string filePath, string fileName)
        {
            if (filePath.Equals(""))
            {
                //Assembly Asm = Assembly.GetExecutingAssembly();
                ////获取配置文件的路径
                //filePath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1));
                filePath = errorFilePath;
            }
            if (!System.IO.File.Exists(filePath + fileName + ".txt"))
            {
                filePathName = filePath + fileName + ".txt";
                System.IO.FileStream f = System.IO.File.Create(filePath + fileName + ".txt");
                f.Close();
            }
        }
        /// <summary>
        /// 向已经存在的文件写入内容
        /// </summary>
        /// <param name="fileContent"></param>
        public static void WriteTxtFile(string fileContent)
        {
            if (filePathName.Equals(""))
            {
                string fileName = "error" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "01.log";
                if (DateTime.Now.Day >= 15)
                {
                    fileName = "error" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "15.log";
                }
                //Assembly Asm = Assembly.GetExecutingAssembly();
                ////获取配置文件的路径
                //filePathName = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + fileName;
                filePathName = errorFilePath + fileName;
            }
            if (!System.IO.File.Exists(filePathName))
            {

                System.IO.FileStream f = System.IO.File.Create(filePathName);
                f.Close();
            }

            StreamWriter sw = new StreamWriter(filePathName, true, System.Text.Encoding.GetEncoding("UTF-8"));
            //通过指定字符编码方式可以实现对汉字的支持，否则在用记事本打开查看会出现乱码


            sw.WriteLine(fileContent);
            sw.Flush();
            sw.Close();
        }
        /// <summary>
        /// 向已经存在的文件写入内容
        /// </summary>
        /// <param name="filePath">文件夹目录</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="fileContent">文件的内容</param>
        public static void WriteTxtFile(string filePath, string fileName, string fileContent)
        {
            if (filePath.Equals(""))
            {
                //Assembly Asm = Assembly.GetExecutingAssembly();
                ////获取配置文件的路径
                //filePath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1));

                filePath = errorFilePath;
            }
            if (!System.IO.File.Exists(filePath + fileName + ".txt"))
            {
                System.IO.FileStream f = System.IO.File.Create(filePath + fileName + ".txt");
                f.Close();
            }

            StreamWriter sw = new StreamWriter(filePath + fileName + ".txt", true, System.Text.Encoding.GetEncoding("UTF-8"));
            //通过指定字符编码方式可以实现对汉字的支持，否则在用记事本打开查看会出现乱码


            sw.WriteLine(fileContent);
            sw.Flush();
            sw.Close();
        }
        /// <summary>
        /// 向指定的文件写入内容
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="Strings"></param>
        public static void WriteFile(string Path, string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();

            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, false, System.Text.Encoding.GetEncoding("UTF-8"));
            f2.Write(Strings);
            f2.Close();
            f2.Dispose();
        }
        /// <summary>
        /// 追加文件
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="strings"></param>
        public static void FileAdd(string Path, string strings)
        {

            StreamWriter sw = File.AppendText(Path);

            sw.Write(strings);

            sw.Flush();

            sw.Close();

        }



        /// <summary>
        /// 拷贝文件
        /// </summary>
        /// <param name="OrignFile">原始文件</param>
        /// <param name="NewFile">新文件路径</param>
        public static void FileCoppy(string orignFile, string NewFile)
        {
            File.Copy(orignFile, NewFile, true);
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="Path">路径</param>
        public static void FileDel(string Path)
        {
            File.Delete(Path);
        }


        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="OrignFile">原始路径</param>
        /// <param name="NewFile">新路径</param>
        public static void FileMove(string orignFile, string NewFile)
        {

            File.Move(orignFile, NewFile);

        }



        /// <summary>
        /// 在当前目录下创建目录
        /// </summary>
        /// <param name="OrignFolder">当前目录</param>
        /// <param name="NewFloder">新目录</param>
        public static void FolderCreate(string orignFolder, string NewFloder)
        {
            Directory.SetCurrentDirectory(orignFolder);
            Directory.CreateDirectory(NewFloder);
        }


        /// <summary>
        /// 递归删除文件夹目录及文件
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static void DeleteFolder(string dir)
        {

            if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
            {

                foreach (string d in Directory.GetFileSystemEntries(dir))
                {

                    if (File.Exists(d))
                        File.Delete(d); //直接删除其中的文件 
                    else
                        DeleteFolder(d); //递归删除子文件夹 
                }
                Directory.Delete(dir); //删除已空文件夹 
            }

        }


        /// <summary>
        /// 指定文件夹下面的所有内容copy到目标文件夹下面
        /// </summary>
        /// <param name="srcPath">原始路径</param>
        /// <param name="aimPath">目标文件夹</param>
        public static void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之

                if (!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath);
                // 得到源目录的文件列表，该里面是包含文件以及目录路径的一个数组

                //如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                //string[] fileList = Directory.GetFiles(srcPath);
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                //遍历所有的文件和目录

                foreach (string file in fileList)
                {

                    //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    //否则直接Copy文件
                    else
                        File.Copy(file, aimPath + Path.GetFileName(file), true);

                }

            }

            catch (Exception ee)
            {

                throw new Exception(ee.ToString());

            }
        }
    }
}
